using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using log4net;
using log4net.Config;
using System.Configuration;

namespace Scenario.Modeler
{
	/// <summary>
	/// Descrizione di riepilogo per FormXML.
	/// </summary>
	public class FormXML : System.Windows.Forms.Form
	{
		private string sXML = string.Empty;
		private bool bOK = false;

		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtXMLFile;
		private System.Windows.Forms.Button button3;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private IContainer components;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem openInXMLToolToolStripMenuItem;
        private ToolStripMenuItem importFromFileToolStripMenuItem;
        private Panel panel3;
        private TextBox txtXML;


		private static readonly ILog Logger= LogManager.GetLogger(typeof(FormXML));

		public FormXML()
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();

			//
			// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
            //
            #region Retrieve temp Directory
            String sTempDirectory = "c:\\temp";
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            try
            {
                AppSettingsSection appSettingSection =
                    (AppSettingsSection)config.GetSection("appSettings");

                sTempDirectory = appSettingSection.Settings["tempDirectory"].Value;
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
            }
            #endregion

            this.txtXMLFile.Text = String.Format("{0}\\{1}.xml", sTempDirectory, Guid.NewGuid().ToString());
           
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.panel1 = new System.Windows.Forms.Panel();
            this.button3 = new System.Windows.Forms.Button();
            this.txtXMLFile = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.openInXMLToolToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.importFromFileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.panel3 = new System.Windows.Forms.Panel();
            this.txtXML = new System.Windows.Forms.TextBox();
            this.panel2.SuspendLayout();
            this.panel1.SuspendLayout();
            this.contextMenuStrip1.SuspendLayout();
            this.panel3.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel2
            // 
            this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel2.Controls.Add(this.btnCancel);
            this.panel2.Controls.Add(this.btnSave);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 342);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(552, 49);
            this.panel2.TabIndex = 15;
            // 
            // btnCancel
            // 
            this.btnCancel.Dock = System.Windows.Forms.DockStyle.Left;
            this.btnCancel.Location = new System.Drawing.Point(0, 0);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(60, 47);
            this.btnCancel.TabIndex = 10;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnSave
            // 
            this.btnSave.Dock = System.Windows.Forms.DockStyle.Right;
            this.btnSave.Location = new System.Drawing.Point(490, 0);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(60, 47);
            this.btnSave.TabIndex = 9;
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // panel1
            // 
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.Add(this.button3);
            this.panel1.Controls.Add(this.txtXMLFile);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(552, 57);
            this.panel1.TabIndex = 17;
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(498, 27);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(40, 20);
            this.button3.TabIndex = 15;
            this.button3.Text = "...";
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // txtXMLFile
            // 
            this.txtXMLFile.Location = new System.Drawing.Point(7, 28);
            this.txtXMLFile.Name = "txtXMLFile";
            this.txtXMLFile.Size = new System.Drawing.Size(485, 20);
            this.txtXMLFile.TabIndex = 14;
            this.txtXMLFile.Text = "textBox1";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(7, 11);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(200, 14);
            this.label1.TabIndex = 0;
            this.label1.Text = "XML File";
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.openInXMLToolToolStripMenuItem,
            this.importFromFileToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(168, 48);
            // 
            // openInXMLToolToolStripMenuItem
            // 
            this.openInXMLToolToolStripMenuItem.Name = "openInXMLToolToolStripMenuItem";
            this.openInXMLToolToolStripMenuItem.Size = new System.Drawing.Size(167, 22);
            this.openInXMLToolToolStripMenuItem.Text = "Open in XML Tool";
            this.openInXMLToolToolStripMenuItem.Click += new System.EventHandler(this.openInXMLToolToolStripMenuItem_Click);
            // 
            // importFromFileToolStripMenuItem
            // 
            this.importFromFileToolStripMenuItem.Name = "importFromFileToolStripMenuItem";
            this.importFromFileToolStripMenuItem.Size = new System.Drawing.Size(167, 22);
            this.importFromFileToolStripMenuItem.Text = "Import from File";
            this.importFromFileToolStripMenuItem.Click += new System.EventHandler(this.importFromFileToolStripMenuItem_Click);
            // 
            // panel3
            // 
            this.panel3.Controls.Add(this.txtXML);
            this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel3.Location = new System.Drawing.Point(0, 57);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(552, 285);
            this.panel3.TabIndex = 19;
            // 
            // txtXML
            // 
            this.txtXML.ContextMenuStrip = this.contextMenuStrip1;
            this.txtXML.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtXML.Location = new System.Drawing.Point(0, 0);
            this.txtXML.Multiline = true;
            this.txtXML.Name = "txtXML";
            this.txtXML.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtXML.Size = new System.Drawing.Size(552, 285);
            this.txtXML.TabIndex = 0;
            // 
            // FormXML
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(552, 391);
            this.ControlBox = false;
            this.Controls.Add(this.panel3);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.panel2);
            this.MinimizeBox = false;
            this.Name = "FormXML";
            this.Text = "XML";
            this.panel2.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.contextMenuStrip1.ResumeLayout(false);
            this.panel3.ResumeLayout(false);
            this.panel3.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		public string XML
		{
			get { return sXML;}
			set { sXML = value; this.txtXML.Text = sXML;}
		}


		public bool OK
		{
			get { return bOK;}
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			sXML = this.txtXML.Text;
			bOK = true;
			this.Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			bOK = false;
			this.Close();
		}

		private void ImportFile ()
		{
			try
			{
				System.IO.StreamReader sw = new System.IO.StreamReader(this.txtXMLFile.Text);
				this.txtXML.Text = sw.ReadToEnd();
				sw.Close();
			}
			catch ( System.IO.FileNotFoundException )
			{
				throw new bOSdotNET.Exception.bOSException("Impossible to find XML file");
			}
			catch ( System.Exception )
			{
				throw new bOSdotNET.Exception.bOSException("Unknow Err.");
			}
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			openFileDialog1.Filter = "xml files (*.xml)|*.xml";
			openFileDialog1.FilterIndex = 1 ;
			openFileDialog1.RestoreDirectory = true ;

			if(openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				this.txtXMLFile.Text = this.openFileDialog1.FileName;

				if ( MessageBox.Show ("Do you want import file?", "Scenario Modeler", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==  DialogResult.Yes )
				{
					this.ImportFile();
				}
			}
		}

        private void openInXMLToolToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /*string sXMLReader = string.Empty;
            try
            {
                ScenarioFramework.Modeler.Settings.XmlManagerTool toolConfig =
                    (ScenarioFramework.Modeler.Settings.XmlManagerTool)
                        System.Configuration.ConfigurationManager.GetSection("scenarioModeler/externalTools/xmlManager");

                sXMLReader = toolConfig.Path;
            }
            catch (System.Exception err)
            {
                MessageBox.Show(err.Message);
                return;
            }
            

            if ((sXMLReader == null) || (sXMLReader == string.Empty))
            {
                MessageBox.Show("Impossible to display XML because XMLReader tool is not setted.");
                return;
            }

            try
            {
                System.IO.StreamWriter sw = System.IO.File.CreateText(this.txtXMLFile.Text);
                sw.Write(this.txtXML.Text);
                sw.Flush();
                sw.Close();
            }
            catch (System.IO.IOException)
            {
                throw new bOSdotNET.Exception.bOSException("Impossible to save XML file");
            }
            catch (System.Exception)
            {
                throw new bOSdotNET.Exception.bOSException("Unknow Err.");
            }
            

            bOSdotNET.Microsoft.Windows.ProcessHandler.Start(sXMLReader, null, this.txtXMLFile.Text);
            */
            /*try
            {
                System.IO.StreamReader sw = System.IO.File.OpenText (this.txtXMLFile.Text);
                this.txtXML.Text = sw.ReadToEnd();
                sw.Close();
            }
            catch (System.IO.IOException)
            {
                throw new bOSdotNET.Exception.bOSException("Impossible to open XML file");
            }
            catch (System.Exception)
            {
                throw new bOSdotNET.Exception.bOSException("Unknow Err.");
            }

            System.IO.File.Delete(this.txtXMLFile.Text);
            */
        }
      

        private void importFromFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ImportFile();	
        }
          
	}
}
